package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CheckItemServiceImpl
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/8/20 16:00
 * @Version V1.0
 */
// dubbo（整合声明式事务处理1：配置applicationContext-tx.xml对类代理，2：@Service(interfaceClass = CheckItemService.class)）
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    OrderSettingDao orderSettingDao;

    @Override
    public void addOrderSetting(List<OrderSetting> orderSettingsList) {
        // 1：遍历List<OrderSetting>
        for (OrderSetting orderSetting : orderSettingsList) {
            // 判断当前的日期之前是否已经被设置过，使用当前时间作为条件查询数量
            long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
            // 如果设置过，更新number数量
            if(count>0){
                orderSettingDao.updateNumberByOrderDate(orderSetting);
            }
            // 如果没有设置过，执行保存
            else{
                // 保存
                orderSettingDao.add(orderSetting);
            }

        }
    }

    /**  传递的参数
     *   date（格式：2019-8）
     *  构建的数据List<Map>
     *    map.put(“date”,1);
          map.put(“number”,120);
          map.put(“reservations”,10);

     *  查询方案：SELECT * FROM t_ordersetting WHERE orderDate LIKE '2019-08-%'
     *  查询方案：SELECT * FROM t_ordersetting WHERE orderDate BETWEEN '2019-9-1' AND '2019-9-31'
     */
    @Override
    public List<Map> findOrderSettingByDate(String date) {
        // 方案二
        // 组织查询条件
        String beginDate = date+"-1";
        String endDate = date+"-31";
        Map<String,Object> paramsMap = new HashMap<>();
        paramsMap.put("beginDate",beginDate);
        paramsMap.put("endDate",endDate);
        // 使用日期的范围完成查询，初始化当前月的数据
        List<OrderSetting> list = orderSettingDao.findOrderSettingByDateBetween(paramsMap);
        // 封装数据结果到List<Map>
        List<Map> resultList = new ArrayList<Map>();
        // 遍历list
        for (OrderSetting orderSetting : list) {
            Map map = new HashMap();
            map.put("date",orderSetting.getOrderDate().getDate());
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());
            resultList.add(map);
        }
        return resultList;
    }

    @Override
    public void updateNumber(OrderSetting orderSetting) {
        // 判断当前的日期之前是否已经被设置过，使用当前时间作为条件查询数量
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        // 如果设置过，更新number数量
        if(count>0){
            orderSettingDao.updateNumberByOrderDate(orderSetting);
        }
        // 如果没有设置过，执行保存
        else{
            // 保存
            orderSettingDao.add(orderSetting);
        }
    }
}
